﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NBlock.MVCExtensions;
using Cost.Models;
using NBlock.Core;
using NBlock.Core.ORM;
using System.Linq.Expressions;
using Cost.Services;

namespace Cost.Controllers
{
    public class DishRangeController : Controller
    {
        //public string[] MealType = new string[] { "大锅菜", "商务套餐", "风味小吃", "面档浇头", "盖浇饭", "晚餐" };

        #region 构造函数
        private readonly ICostRepository<DishRange> dishRangeRepository;
        private readonly ICostRepository<DishRangeView> dishRangeViewRepository;
        private readonly ICostRepository<DishOrderItem> dishOrderItemRepository;
        private readonly IEnumService enumService;
        private readonly CommonService commonService;
        public DishRangeController(ICostRepository<DishRange> dishRangeRepository,
            ICostRepository<DishRangeView> dishRangeViewRepository,
            ICostRepository<DishOrderItem> dishOrderItemRepository,
            IEnumService enumService,
            CommonService commonService)
        {
            this.dishRangeRepository = dishRangeRepository;
            this.dishRangeViewRepository = dishRangeViewRepository;
            this.dishOrderItemRepository = dishOrderItemRepository;
            this.enumService = enumService;
            this.commonService = commonService;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult AddDish(DateTime date, string MealType, IList<Dish> dishList)
        {
            foreach (var dish in dishList)
            {
                var range = dishRangeRepository.Find(r => r.Date == date && r.DishId == dish.Id && r.MealType == MealType);
                if (range == null)
                {
                    range = new DishRange
                    {
                        Date = date,
                        DishId = dish.Id,
                        DishCode = dish.Code,
                        DishName = dish.Name,
                        MealType = MealType,
                    };
                    dishRangeRepository.Save(range);
                }
            }
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult Delete(string Id)
        {
            dishRangeRepository.Delete(Id);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult DeleteAll(string Ids)
        {
            var items = Ids.Split(',');
            dishRangeRepository.DeleteAll(d => items.Contains(d.Id));
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult List(DateTime? date, nbPageInfo pager, nbSearchCondition where)
        {
            Expression<Func<DishRangeView, bool>> filter = null;
            if (date.HasValue)
            {
                filter = d => d.Date == date.Value;
            }

            var pageData = dishRangeViewRepository.GetPageable(pager, filter, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        [HttpPost]
        public JsonResult Tree(string Id)
        {
            var nodes = new List<nbJsonTreeNode>();

            // 获取下周的当天及当前是周几
            var NextMonday = CommonLogic.GetNextMonday();
            var items = enumService.GetEnumDataSource("Week");
            foreach (var item in items)
            {
                // 将星期几换算为当前的日期
                var date = NextMonday.AddDays(double.Parse(item.Value) - 1).ToString("yyyy-MM-dd");
                nodes.Add(new nbJsonTreeNode
                {
                    id = date,
                    text = date + string.Format("（{0}）", item.Text),
                    value = date,
                });
            }
            return Json(nodes);
        }

        #region LookUp
        public ActionResult LookUp(string orderId, DateTime date)
        {
            ViewBag.OrderId = orderId;
            ViewBag.Date = date;
            return View();
        }

        [HttpPost]
        public JsonResult LookUpList(string orderId, DateTime date, nbPageInfo pager, nbSearchCondition where)
        {
            // 查找当前项目点的订单明细
            var existDishes = dishOrderItemRepository
                .FindAll(item => item.OrderId == orderId)
                .Select(item => new { item.DishId, item.MealType });

            var pageData = dishRangeViewRepository.GetPageable(pager,
                d => d.Date == date && !existDishes.Any(a => a.DishId == d.DishId && a.MealType == d.MealType), where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
        #endregion

        #region 历史大菜单
        public ActionResult History()
        {
            return View();
        }

        [HttpPost]
        public JsonResult HistoryList(nbPageInfo pager, nbSearchCondition where)
        {
            var pageData = dishRangeViewRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
        #endregion
    }
}
